package com.fastCms.manager.frame.controller;


import com.fastCms.business.frame.busi.CloudRoleManageService;
import com.fastCms.business.frame.exception.BusinessException;
import com.fastCms.business.frame.pojo.po.CloudRolePO;
import com.fastCms.business.frame.pojo.po.CloudRoleSavePO;
import com.fastCms.business.frame.pojo.vo.CloudRoleVO;
import com.fastCms.business.frame.pojo.vo.UserSimpleVO;
import com.fastCms.business.frame.response.ApiResponse;
import com.fastCms.business.frame.response.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 平台角色表 前端控制器
 * </p>
 *
 * @author liusq
 * @since 2019-10-15
 */
@RestController
@RequestMapping("/cloudRole")
@Api(value = "CloudRoleController", tags = "后端-角色管理中心")
public class CloudRoleController {

    @Autowired
    private CloudRoleManageService cloudRoleManageServiceImpl;

    @PostMapping("/showRoles")
    @ApiOperation(value = "查询角色列表", notes = "只展示普通角色")
    public ApiResponse<List<CloudRoleVO>> showRoles() throws BusinessException {
        List<CloudRoleVO> cloudRoles = cloudRoleManageServiceImpl.showRoles();
        return ResponseUtil.success(cloudRoles);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "角色新增或修改", notes = "修改基本信息和关联菜单权限1.id为空，新增操作 2.id不为空，修改操作")
    public ApiResponse<Boolean> saveOrUpdate(@Valid @RequestBody CloudRolePO cloudRolePO) throws BusinessException {
        return ResponseUtil.success(cloudRoleManageServiceImpl.saveOrUpdateRole(cloudRolePO));
    }

    @GetMapping("/delete/{roleId}")
    @ApiOperation(value = "删除角色-byRoleId", notes = "角色下有用户时，不能删除")
    public ApiResponse<Boolean> delete(@PathVariable String roleId) throws BusinessException {
        return ResponseUtil.success(cloudRoleManageServiceImpl.deleteRoleAndMenuReal(roleId));
    }

    @GetMapping("/showUserList/{roleId}")
    @ApiOperation(value = "查询角色下所对应的人员-byRoleId", notes = "根据查询角色id所授权的人员列表")
    public ApiResponse<List<UserSimpleVO>> showUserList(@PathVariable String roleId) {
        return ResponseUtil.success(cloudRoleManageServiceImpl.findUserListByRoleId(roleId));
    }


}

